package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.print.attribute.standard.PagesPerMinute;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Pageable;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 0：乱码
         * 1.得到前端的数据
         * 2.封装数据到对象
         * 3.调用服务逻辑层
         * 4.得到服务逻辑层返回来的数据
         * 5.将数据返回给前端
         */
//        0：乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String weight = req.getParameter("weight");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        String startBirthday = req.getParameter("startBirthday");
        String endBirthday = req.getParameter("endBirthday");
        String enable = req.getParameter("enable");
        String curPageString = req.getParameter("curPage");
        String pageSizeString = req.getParameter("pageSize");
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
        userModel.setPhone(phone);
        userModel.setWeight(weight);
        userModel.setEmail(email);
        userModel.setSex(sex);
        userModel.setHobby(hobby);
        userModel.setStartBirthday(startBirthday);
        userModel.setEndBirthday(endBirthday);
        userModel.setEnable(Tool.stringToInt(enable));
        PageModel pageModel = new PageModel();
        pageModel.setCurPage(Tool.stringToInt(curPageString));
        pageModel.setPageSize(Tool.stringToInt(pageSizeString));
        userModel.setPageModel(pageModel);
        //3.调用服务逻辑层
        IUserService iUserService = new UserServiceImpl();
        List<UserModel> list = iUserService.findAll(userModel);
        //        5.将数据返回给前端,必须以json的格式传递{key:value,key:value}
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        int count = iUserService.getCount(userModel);

        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
